package com.lilicould.controller;

import com.lilicould.entity.ContractApartment;
import com.lilicould.entity.ContractTenant;
import com.lilicould.entity.LeaseContract;
import com.lilicould.service.impl.ContractServiceImpl;
import com.lilicould.service.impl.HouseServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/contract/add")
public class ContractAddController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String signDate = req.getParameter("signDate");
        String startDate = req.getParameter("startDate");
        int leaseTerm = Integer.parseInt(req.getParameter("leaseTerm"));
        double deposit =  Double.parseDouble(req.getParameter("deposit"));
        double rent = Double.parseDouble(req.getParameter("rent"));
        String managerId = req.getParameter("managerId");
        String buildingId = req.getParameter("buildingId");
        String apartmentNo = req.getParameter("apartmentNo");
        String tenantId = req.getParameter("tenantId");

        // 封装到对应的实体类中
        LeaseContract leaseContract = new LeaseContract(null, signDate, startDate, leaseTerm, deposit, rent, managerId);
        ContractApartment contractApartment = new ContractApartment(null, buildingId, apartmentNo);
        ContractTenant contractTenant = new ContractTenant(null, tenantId);

        // 获取服务层接口
        ContractServiceImpl contractService = new ContractServiceImpl();
        HouseServiceImpl houseService = new HouseServiceImpl();

        // 判断是否是该楼栋的管理经理
        if (!contractService.isYours(managerId, buildingId)) {
            req.setAttribute("error", "您没有权限创建此合同,因为此楼栋不归您管理");
            req.getRequestDispatcher("/page/err.jsp").forward(req, resp);
            return;
        }

        // 判断此公寓是否存在
        if (!houseService.isExist(buildingId, apartmentNo)){
            req.setAttribute("error", "此公寓不存在");
            req.getRequestDispatcher("/page/err.jsp").forward(req, resp);
            return;
        }

        // 判断此楼栋是否已被出租
        String leasedContractId = contractService.isLeased(buildingId, apartmentNo);
        if (leasedContractId != null) {
            req.setAttribute("error", "此公寓已被出租,出租合同编号为："+leasedContractId+"请前往租赁合同列表添加租客");
            req.getRequestDispatcher("/page/err.jsp").forward(req, resp);
            return;
        }

        // 添加合同
        boolean result = contractService.addContract(leaseContract, contractApartment, contractTenant);

        if (result) {
            req.setAttribute("success", "创建成功");
            req.getRequestDispatcher("/page/success.jsp").forward(req, resp);
        } else {
            req.setAttribute("error", "创建失败");
            req.getRequestDispatcher("/page/err.jsp").forward(req, resp);
        }





    }
}
